package sweepmine;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Record {

    BufferedWriter bf;
    BufferedReader br;
    FileWriter out;
    FileReader in;

    public Record() {

    }

    // 写入存档基础数据
    public void writeBasicData(int[] data) {
        try {
            out = new FileWriter("record//basicdata.txt", true);
            bf = new BufferedWriter(out);
            int l = data.length;
            for (int i = 0; i < l; i++)
                bf.write(data[i] + "<>");
            bf.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 写入存档二维数组数据
    public void writeArray(int type, int[][] data, int r, int c) {
        try {
            switch (type) {
                case 0:
                    out = new FileWriter("record//save.txt", true);
                    break;// 写入存档save矩阵
                case 1:
                    out = new FileWriter("record//isMine.txt", true);
                    break;// 写入存档isMine矩阵
                case 2:
                    out = new FileWriter("record//isEnable.txt", true);
                    break;// 写入存档isEnable矩阵
                case 3:
                    out = new FileWriter("record//rightClick.txt", true);
                    break;// 写入存档rightClick矩阵
                case 4:
                    out = new FileWriter("record//right.txt", true);
                    break;// 写入存档right矩阵
            }
            bf = new BufferedWriter(out);
            for (int i = 0; i < r; i++) {
                for (int j = 0; j < c; j++) {
                    bf.write(data[i][j] + "<>");
                }
            }
            bf.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void write(int type, int level, int i) {
        try {
            switch (type) {
                case 0:
                    out = new FileWriter("record//number" + (level + 1) + ".txt", true);
                    break; // 写入局数 writeNumber()
                case 1:
                    out = new FileWriter("record//Level" + (level + 1) + ".txt", true);
                    break; // 写入扫雷用时 writeTime()
                case 2:
                    out = new FileWriter("record//level.txt", true);
                    break; // 写入扫雷级别 writeLevel()
            }
            bf = new BufferedWriter(out);
            switch (type) {
                case 0:
                    bf.write(i + "<>");
                    break; // 写入局数 writeNumber()
                case 1:
                    bf.write(i + "<>" + getTime() + "<>");
                    break; // 写入扫雷用时 writeTime()
                case 2:
                    bf.write(i + "");
                    break; // 写入扫雷级别 writeLevel()
            }
            bf.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String read(int type, int level) {
        String s = "";
        int c;
        try {
            switch (type) {
                case 0:
                    in = new FileReader("record//number" + (level + 1) + ".txt");
                    break; // 读取局数 readNumber()
                case 1:
                    in = new FileReader("record//Level" + (level + 1) + ".txt");
                    break; // 读取扫雷用时 readTime()
                case 2:
                    in = new FileReader("record//level.txt");
                    break; // 读取扫雷级别 readLevel()
                case 3:
                    in = new FileReader("record//basicdata.txt");
                    break;// 读取存档基础数据
                case 4:
                    in = new FileReader("record//save.txt");
                    break;// 读取存档save矩阵
                case 5:
                    in = new FileReader("record//isMine.txt");
                    break;// 读取存档isMine矩阵
                case 6:
                    in = new FileReader("record//isEnable.txt");
                    break;// 读取存档isEnble矩阵
                case 7:
                    in = new FileReader("record//rightClick.txt");
                    break;// 读取存档rightClick矩阵
                case 8:
                    in = new FileReader("record//right.txt");
                    break;// 读取存档right矩阵
            }
            br = new BufferedReader(in);
            while ((c = br.read()) != -1) {
                s = s + (char) c;
            }
            br.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    // 清除存档数据
    public void clearsave() {
        try {
            out = new FileWriter("record//basicdata.txt");
            out.write("");
            out.close();
            out = new FileWriter("record//save.txt");
            out.write("");
            out.close();
            out = new FileWriter("record//isMine.txt");
            out.write("");
            out.close();
            out = new FileWriter("record//isEnable.txt");
            out.write("");
            out.close();
            out = new FileWriter("record//rightClick.txt");
            out.write("");
            out.close();
            out = new FileWriter("record//right.txt");
            out.write("");
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 清空记录
    public void clear() {
        try {
            for (int i = 1; i < 4; i++) {
                out = new FileWriter("record//number" + i + ".txt");
                out.write("");
                out.close();
                out = new FileWriter("record//Level" + i + ".txt");
                out.write("");
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取现在时间
    public String getTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 完整的时间
        String time = sdf.format(date);
        return time;
    }
}
